package redis.gambing;

import org.apache.storm.task.OutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.base.BaseRichBolt;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Tuple;
import org.apache.storm.tuple.Values;

import java.util.Map;
import java.util.Random;

import static redis.gambing.GambleFinalString.*;

/**
 * 生成一次赌博的结果
 */
public class GambleResult extends BaseRichBolt {
    private OutputCollector collector;
    private final Random random = new Random();
    @Override
    public void prepare(Map<String, Object> conf, TopologyContext context, OutputCollector collector) {
        this.collector = collector;
    }

    @Override
    public void execute(Tuple input) {
        boolean gambleResult = random.nextBoolean();
        collector.emit(new Values(input.getStringByField(GAMBLER_ID), input.getLongByField(HASH_GAMBLER_1_MONEY), input.getShortByField(HASH_GAMBLER_2_TIMES), input.getLongByField(HASH_GAMBLER_3_ASSERTS), gambleResult));
    }

    @Override
    public void declareOutputFields(OutputFieldsDeclarer declarer) {
        declarer.declare(new Fields(GAMBLER_ID, HASH_GAMBLER_1_MONEY, HASH_GAMBLER_2_TIMES, HASH_GAMBLER_3_ASSERTS, GAMBLE_RESULT));
    }
}
